package com.dbn.webmagic.pageprocessor;

import com.alibaba.fastjson.JSONObject;
import com.dbn.webmagic.utils.UrlDetail;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;
import us.codecraft.webmagic.utils.HttpConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liheping
 * @description  抓取品种审定网站数据内容
 * @date 2021/11/11
 */

@Component
public class VarietyProcessor implements PageProcessor {

    private int flag=0;//标识，翻页爬所有url只执行一次
    @SneakyThrows
    @Override
    public void process(Page page) {

        //查询列表数据
        if(page.getUrl().regex(UrlDetail.AUTHLIST_URL).match()){
            //翻页爬取所有
            if(flag==0){
                String total=new JsonPathSelector("$.total").select(page.getRawText());
                if(Integer.parseInt(total)==0)
                    return;
                int pageNum=Integer.parseInt(total);
                if(pageNum>1){
                    String JudgementNo=(String) page.getRequest().getExtra("JudgementNo");
                    String VarietyName=(String) page.getRequest().getExtra("VarietyName");
                    String ApplyCompany=(String) page.getRequest().getExtra("ApplyCompany");
                    String CropID=(String) page.getRequest().getExtra("CropID");
                    String IsTransgenosis=(String) page.getRequest().getExtra("IsTransgenosis");
                    String JudgementRegionID=(String) page.getRequest().getExtra("JudgementRegionID");
                    String introductionFilingType=(String) page.getRequest().getExtra("introductionFilingType");
                    for(int i=0;i<=pageNum;i++){
                        String nexturl=UrlDetail.AUTHFIRST+JudgementNo+"&VarietyNameLike="+VarietyName+"&JudgementYear=&ApplyCompanyLike="+ApplyCompany+"&CropID="+CropID+"&IsTransgenosis="+IsTransgenosis+"&IsExit=&JudgementRegionID="+JudgementRegionID+"&introductionFilingType="+introductionFilingType+"&_search=false&nd=1636427922524&rows=20&page="+(i+1)+"&sidx=JudgementYear+desc+%2CRegionID%2CJudgementNo&sord=desc";;
                        page.addTargetRequest(nexturl);
                    }
                }
                flag=1;
            }
                List<String> list = new JsonPathSelector("$.rows[*]").selectList(page.getRawText());
                if(list.size()==0){
                    return;
                }
                for (String json : list) {
                    JSONObject jsonObject = JSONObject.parseObject(json);
                    String JudgementNo=jsonObject.getString("JudgementNo");
                    String VarietyName=jsonObject.getString("VarietyName");
                    String CompanyHasLincense=jsonObject.getString("CompanyHasLincense");
                    String applycompany=jsonObject.getString("ApplyCompany");
                    //查询许可信息详情
                    if(CompanyHasLincense.equals("1")){
                        Map<String, Object> params = new HashMap<String, Object>();
                        params.put("companyName",applycompany);
                        params.put("judgementNo",JudgementNo);
                        Request request=new Request(UrlDetail.LINCENSEPIPE_URL);
                        request.setMethod(HttpConstant.Method.POST);
                        //用于网页之间传参
                        request.setExtras(params);
                        request.setRequestBody(HttpRequestBody.form(params, "utf-8"));
                        page.addTargetRequest(request);
                        //将未爬取的审定详情添加到list中
                    }
                        //拼接url
                        String url=UrlDetail.ANNOINFOPIPE_URL+"?judgementNo="+JudgementNo+"&VarietyName="+VarietyName;
                        page.addTargetRequest(url);
                }
            }
        if(page.getUrl().regex(UrlDetail.ANNOINFO_URL).match()){
                System.out.println("===========================================>申定详情");
                page.putField("annoInfo",page.getJson().get());
            }
        //许可详情
        if(page.getUrl().regex(UrlDetail.LINCENSE_URL).match()){
            System.out.println("==================>>>>>>>>>>>许可信息");
            String judgementNo= (String) page.getRequest().getExtra("judgementNo");
            page.putField("lincense",page.getJson().get());
            page.putField("judgementNo",judgementNo);
            }
    }

    @Override
    public Site getSite() {
        return Site.me().setSleepTime(100);
    }
}
